import Vue 			from 'vue'
import util 		from '../../util'
import axios 		from 'axios'
import { Toast,Dialog,Loading  } 	from 'vant';
Vue.use(Toast,Dialog,Loading );

export default {
	name: 'moneyAgreement',
	data(){
		return {
            recruitShowArr: [],     //要展示的任务列表
            recruitList: [],        //任务列表
            isNovice: true,         //是否新手
            maxNumber: 66,          //最高领取-暂定写死
            isLoading: true,        //数据加载中
            helpLink: 'https://support.deepcoin.pro/hc/zh-cn/categories/360003941211',           //帮助中心连接
            endTime: '',            //注册时间
            timeStr: '',            //倒计时时间
            timeObj: null,          //倒计时对象
		}
	},
	methods:{
		init(type){
            //登录-判断是否新手
            if(this.token || util.getToken()) {
                this.getTaskStatus()
            } else {
                //未登录-展示新手
                this.getRecruitList()
            }
        },

        //设置倒计时
        setTimeFun(time) {
            let _this = this
            clearTimeout(this.timeObj)
            this.timeObj = setTimeout(() => {
                time = time - 1000
                this.timeStr = util.transTime5(time)
                this.setTimeFun(time)
            }, 1000)
        },

        //任务状态-新手||进阶
        getTaskStatus() {
            util.fetch(`${util.ajaxUrl}/v1/task/task-status`, {}).then(res => {
                let {retCode = -1, retData = [], retMsg ='' } = res || {}
                if(retCode == 0 && retData) { 
                    this.isNovice = retData.task_process == 0
                    let tit = this.isNovice ? '新手任务' : '进阶任务'
                    util.callHandler('setNavTitle',{
                        title:tit
                    })
                    if(retData.task_process == 0) {
                        this.getRecruitList()
                    } else {
                        this.getAdvanced()
                    }
                } else {
                    let msg = retMsg == 'error' ? '请求失败，请稍后再试' : retMsg
                    Toast(msg)
                    this.isLoading = false
                }
            })
        },

        //获取任务列表-新手
        getRecruitList() {
            util.callHandler('setNavTitle',{
                title:'新手任务'
            })
            util.fetch(`${util.ajaxUrl}/v1/task/recruit-list`,{}).then(res => {
                let {retCode = -1, retData = [] } = res || {}
                if(retCode == 0 && retData) {
                    this.recruitShowArr = retData.list
                    if(retData.reg_time && retData.reg_time >0 ) {
                        let getTime = (retData.reg_time * 1000 + (86400000 * 3)) - (new Date().getTime())
                        getTime > 0 && this.setTimeFun(getTime)
                    }
                }
                this.isLoading = false
            })
        },

        //获取进阶任务列表
        getAdvanced() {
            util.fetch(`${util.ajaxUrl}/v1/task/advanced-list`,{}).then(res => {
                let {retCode = -1, retData = [] } = res || {}
                if(retCode == 0 && retData) {
                    this.recruitShowArr = retData.list
                    console.log(util.transTime5(retData.reg_time))
                }
                this.isLoading = false
            })
        },

        //跳转
        junpFun(url) {
            if(!url) return
            let isLogin = !url.includes('registePage')
            util.callHandler('openUrl',{
                url: url,
                title: '',
                needLogin:isLogin,
                popVC:false,		//跳转的时候是否干掉当前页面
                hiddenNav:false,	//是否全屏
            },res => {
                console.log(res)
            })
        },

		goBack(){
			util.navigateBack(-1,this)
        },
       
	},
	created() {
		util.waitSDKReady().then(token => {
            this.token = token;
			this.init();
			setTimeout(() => {
				util.registerHandler('refreshCommonRequestWhenGoBackView',res => {
					if(this.token) this.init(1);
					util.callHandler('setStatusBarStyle',{
						style:'dark'
					})
				})
				//退出登录的时候native调用H5方法清除token缓存
				util.registerHandler('loginOutDeleteToken',res => {
					window.token = '';
					this.token = '';
					localStorage.removeItem('token');
				})
				util.callHandler('setStatusBarStyle',{
					style:'dark'
				})
			},500)
		}).catch((e) => {
            this.init();
			// Toast('获取token失败')
		})
    }
}